{extend name="/index/base" /}

{block name="content"}
<div class="layui-card">
    <div class="layui-card-header layuiadmin-card-header-auto">
        <h2>修改文章</h2>
        <a  class="layui-btn" href="{:URL('admin/ad/index')}" >返 回</a>
    </div>
    <div class="layui-card-body">
        <form class="layui-form" action="" method="post">
            <div class="layui-form-item">
                <label for="" class="layui-form-label">广告位</label>
                <div class="layui-input-block" style="position: relative;z-index: 10000;">
                    <select name="parent_id" id="parent_id" lay-search lay-verify="required">
                        <option value="">选择广告位</option>
                        {foreach $positions as $k => $v}
                        <option value="{$v['id']}" {if $data['position_id'] == $v['id']} selected {/if}>{$v['name']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="{$data['name']??''}" lay-verify="required" class="layui-input name" placeholder="">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">URL</label>
                <div class="layui-input-block">
                    <input type="text" name="url" value="{$data['url']??''}" lay-verify="" class="layui-input url" placeholder="">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input class="layui-input sort" type="text" name="sort" value="{$data['sort']??''}" placeholder="如：1" >
                </div>
            </div>

            <div class="layui-form-item">
                <label for="" class="layui-form-label">缩略图</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="uploadPic"><i class="layui-icon">&#xe67c;</i>图片上传</button>
                        <div class="layui-upload-list" >
                            <ul id="layui-upload-box" class="layui-clear">
                                {if isset($data['content']) && $data['content']}
                                <li><img src="{$data['content']}" width="300" /><p>上传成功</p></li>
                                {/if}
                            </ul>
                            <input type="hidden" name="cover" id="cover" value="{$data['content']}">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" class="id" value="{$data['id']}" />
                    <span type="submit" id="submit" class="layui-btn" lay-submit="" lay-filter="formDemo">确 认</span>
                    <a  class="layui-btn" href="{:URL('admin/ad/index')}" >返 回</a>
                </div>
            </div>

        </form>
    </div>
</div>

{/block}


{block name="script"}
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var editor = UE.getEditor('container');
</script>

<script>
    function checkSort(val, max){
        var regex = /^\d+$/;
        if(regex.test(val)){
            if(val > max || val <1){
                layer.msg("请输入1-124之间的数字！")
                return false;
            }
        }else{
            layer.msg("请输入1-124之间的数字！");
            return false;
        }
    }
    layui.use(['layer','table','form','upload'],function () {
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.$ //由于layer弹层依赖jQuery，所以可以直接得到

        //默认提交状态为false
        var commitStatus = false;

        form.on("checkbox(allCheck)", function(data){
            if (data.elem.checked) {
                $(".tag").each(function () {
                    $(this).prop('checked', true);
                });
            } else {
                $(".tag").each(function () {
                    $(this).prop('checked', false);
                });
            }
            form.render("checkbox");


        });

        var upload = layui.upload
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadPic'
            ,url: '{:URL("admin/ad/uploads")}'
            ,multiple: false
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#layui-upload-box').html('<li><img src="'+result+'" width="300" /><p>上传中</p></li>')
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code == 0){
                    $("#cover").val(res.url);
                    $('#layui-upload-box li p').text('上传成功');
                    return layer.msg(res.msg);
                }
                return layer.msg(res.msg);
            }
        });

        //搜索
        form.on('submit(formDemo)', function(data2) {
            var name = $(".name").val()
            var sort = $(".sort").val();
            var parent_id = $("#parent_id").val();
            var cover = $("#cover").val();
            var id = $(".id").val();
            var url = $(".url").val();

            var data = {'name':name,'sort':sort,'position_id':parent_id,'content':cover,'id':id,"url":url};

            if(commitStatus==false){
                //提交表单后，讲提交状态改为true
                commitStatus = true;
            }else{
                layer.msg('请勿重复提交');
                return false;
            }

            $.ajax({
                type: 'POST',
                url: '{:url("admin/ad/update")}',
                data: data,
                success: function (ret) {
                    if (ret.code == 200) {
                        layer.msg(ret.msg, {icon: 6, time: 1000}, function () {
                            window.location.href='{:url("admin/ad/index")}';
                        });
                    } else {
                        layer.msg(ret.msg);
                    }
                    return false;
                }
            });
        })

        $(".sort").blur(function(){
            checkSort($(this).val(), 124);
        });

    })
</script>
{/block}
